home *** CD-ROM | disk | FTP | other *** search
/ Softwarová Záchrana 3 / Softwarova-zachrana-3.bin / TextHarvest / TextHarvest-Install.exe / {app} / Help-RegExp.txt < prev    next >
Text File  |  2005-01-04  |  4KB  |  74 lines

  1. <h1>Parse-O-Matic: Regular Expressions</h1>
  2.  
  3. <hl>Note: This is an appendix to the "Parse-O-Matic Scripts" user manual.</hl>
  4.  
  5. <h2>Table of Contents</h2>
  6.  
  7. You can click on any section title below to jump directly to that section.
  8.  
  9.    <a href="#OVERVIEW">Overview</a>
  10.    <a href="#BASICREX">Basic Regular Expressions</a>
  11.    <a href="#ASTERISK">Using the Asterisk</a>
  12.    <a href="#ADVANCRE">Advanced Regular Expressions</a>
  13.  
  14. <a name="OVERVIEW"><h2>Overview</h2>
  15.  
  16. In the following list, the letters x, y and z stand in for any character.
  17. <hl>
  18.   ^xxx    Matches a sequence of characters at the start of a line
  19.   xxx$    Matches a sequence of characters at the end of line
  20.   x.x     Matches a single character
  21.   [xz]    Matches a set of characters ('x' and 'z' in this example)
  22.   [x-z]   Matches a range of characters (this example covers 'x' to 'z')
  23.   x*      Matches zero or more occurrences of the preceding character
  24.   [xyz]*  Matches zero or more occurrences from the preceding set
  25.   [x-z]*  Matches zero or more occurrences from the preceding range
  26.   [^xyz]  Matches any character but the ones specified
  27.   [^x-z]  Matches any character but the ones in the specified range
  28. </hl>
  29. The backslash (\) character has a special meaning in regular expressions:
  30. <hl>
  31.   \x      Means "take the next character literally"
  32.           For example:  \[  means the actual  [  character
  33.           rather than the start of a set or range
  34.   \t      Means "a tab character" (ASCII character 9)
  35. </hl>
  36. <a name="BASICREX"><h2>Basic Regular Expressions</h2>
  37.  
  38. Here are some examples of matches:
  39. <hl>
  40.   C.t        Matches Cat, Cot, Cut, Cxt, C3t etc.
  41.   C[aou]t    Matches Cat, Cot, Cut only
  42.   B..d       Matches Bird, Bred, Bead etc.
  43.   ^Dog       Matches Dog only if it is at the beginning of a line
  44.   Moose$     Matches Moose only if it is at the end of a line
  45.   Pa*d       Matches Pd, Pad, Paad, Paaad etc.
  46. </hl>
  47. <a name="ASTERISK"><h2>Using the Asterisk</h2>
  48.  
  49. The last example given above uses the * character to indicate zero, one or more occurrences of a particular character ù in this case, the letter 'a'. This is <b>different</b> from the way the Windows operating system uses the * wildcard character. In Windows, the * wildcard matches "any single character".
  50.  
  51. In regular expressions, however, the asterisk is specific about what you are looking for. That is why 'Pa*d' would not match 'Parsed'; the asterisk means "match zero or more of the preceding character specification".
  52.  
  53. If you actually want to search for 'Pa' followed by one or more letters and then 'd', the correct syntax is:
  54.  
  55. <hl>  Pa[a-z][a-z]*d</hl>
  56.  
  57. This means that we want to match 'Pa', then a letter in the range from 'a' to 'z', then some number (including zero) of characters in the 'a' to 'z' range, and finally the letter 'd'. The character string 'Parsed' would meet these criteria, as would 'Pad', 'Paid' and 'Packed'.
  58.  
  59. <a name="ADVANCRE"><h2>Advanced Regular Expressions</h2>
  60.  
  61. Here are some more complicated examples of regular expressions:
  62. <hl>
  63.   C[^ou]t        Matches Cat, Cxt and so on, but not Cot or Cut
  64.   C[ao]*t        Matches Ct, Cat, Caat, Cot, Coot, Cooot, Coat, Coaoat etc.
  65.   [0-9][0-9]*    Matches numbers such as 0, 1, 01, 10, 25, 0990, 9999 etc.
  66.   -[0-9][0-9]*   Matches negative numbers such as -0, -1, -19, -12345 etc.
  67. </hl>
  68. In the last example, [0-9] is specified twice to ensure that at least one digit is found. Bear in mind that the * character means "zero or more occurrences". If you had only specified '-[0-9]*' you would get a spurious match within the string 'Hello - there', since the '-' character is indeed found, followed by <b>zero</b> occurrences of the digits 0 through 9.
  69.  
  70. You can create fairly complex patterns using regular expressions. Consider this example:
  71.  
  72. <hl>  \$[0-9][0-9]*\.[0-9][0-9]</hl>
  73.  
  74. This would match dollar amounts with two decimal places, such as $0.00, $03.23, $3.14, $9.99, $1234.56 and so on.